package com.atguigu.gulimall.search.controller;

import com.atguigu.common.exception.BizCodeEnum;
import com.atguigu.common.utils.R;
import com.atguigu.gulimall.search.service.ElasticSaveService;
import com.atguigu.to.es.SkuEsModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

/**
 * @author centurion
 * @description: TODO
 * @date 2024/9/21
 */
@RestController
@RequestMapping("/search/save")
public class ElasticSaveController {

    @Autowired
    private ElasticSaveService elasticSaveService;

    /**
     * 上架商品
     *
     * @return
     */
    @PostMapping("/product")
    public R productUp(@RequestBody List<SkuEsModel> modelList) {
        Boolean b = false;
        try {
            b = this.elasticSaveService.productUp(modelList);
        } catch (IOException e) {
            e.printStackTrace();
            return R.error(BizCodeEnum.PRODUCT_UP_EXCEPTION.getCode(), BizCodeEnum.VALID_EXCEPTION.getMessage());
        }

        if (b) {
            return R.error(BizCodeEnum.PRODUCT_UP_EXCEPTION.getCode(), BizCodeEnum.VALID_EXCEPTION.getMessage());
        } else {

            return R.ok();
        }

    }
}
